package com.rongshu.view.home.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.DateUtils;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.home.contract.DaySignDetailsView;
import com.rongshu.view.home.entity.DaySignDetailsResponse;
import com.rongshu.view.home.entity.DaySignInfo;
import com.rongshu.view.home.presenter.DaySignPresenter;
import com.rongshu.view.home.widget.SelectableTextView;
import com.umeng.socialize.UMShareAPI;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * created by zhengweis on 2019/2/18.
 * description：日签页面
 */
public class DaySignDetailsActivity extends BaseActivity<DaySignPresenter> implements DaySignDetailsView {

    @BindView(R.id.head_middle)
    TextView headMiddle;
    @BindView(R.id.iv_day_sign_img)
    ImageView ivDaySignImg;
    @BindView(R.id.sign_title)
    TextView txtDaySignTitle;
    @BindView(R.id.sign_content)
    SelectableTextView txtDaySignContent;
    @BindView(R.id.sign_date)
    TextView shareDataLayout;
    @BindView(R.id.header)
    RelativeLayout header;
    @BindView(R.id.content)
    LinearLayout content;
    @BindView(R.id.bottom)
    RelativeLayout bottom;
    @BindView(R.id.head_right)
    RelativeLayout headRight;
    @BindView(R.id.sign_ahthor)
    TextView signAuthor;

    private Bitmap shareImg;
    private int signId;
    private int type; //1制作（系统的日期），2编辑（用户的日期）
    private DaySignDetailsResponse.ResponseBean bean;

    @Override
    protected int initLayout() {
        return R.layout.activity_day_sign_details;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        signId = intent.getIntExtra(Constant.INTENT_DATA, 0);
        type = intent.getIntExtra(Constant.INTENT_TYPE, 1);
        headMiddle.setText(type == Constant.DAY_SIGN_AUTHOR ? "我的日签" : "榕书日签");
        headRight.setVisibility(type == Constant.DAY_SIGN_AUTHOR ? View.VISIBLE : View.GONE);
        signAuthor.setText(type == Constant.DAY_SIGN_AUTHOR ? UserInfoUtils.getBiName() : "榕书");
        setPresenter(new DaySignPresenter(this), this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (presenter != null) {
            presenter.getDaySignInfo(type, signId);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }

    @OnClick({R.id.head_left, R.id.head_right, R.id.iv_share, R.id.iv_edit, R.id.iv_day_sign_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.head_left:
                finish();
                break;
            case R.id.head_right:
                presenter.confirmDeleteSign(String.valueOf(signId));
                break;
            case R.id.iv_share:
                DaySignInfo.ResponseBean daySignBean = new DaySignInfo.ResponseBean();
                daySignBean.setDailyDate(bean.getDailyDate());
                daySignBean.setImgUrl(bean.getImgUrl());
                daySignBean.setImgExplain(bean.getImgExplain());
                daySignBean.setDailyContent(bean.getDailyContent());
                daySignBean.setIsSystem(type);
                Intent intent1 = new Intent(this, ShareDaySignActivity.class);
                intent1.putExtra(Constant.INTENT_DATA, daySignBean);
                startActivity(intent1);
                break;
            case R.id.iv_edit:
                Intent intent = new Intent(this, EditDaySignActivity.class);
                intent.putExtra(Constant.INTENT_DATA, signId);
                intent.putExtra(Constant.INTENT_TYPE, type);
                startActivity(intent);
                break;
            case R.id.iv_day_sign_img:
                if (bean != null && !TextUtils.isEmpty(bean.getImgUrl())) {
                    ArrayList<String> listA = new ArrayList<>();
                    listA.add(bean.getImgUrl());
                    intent = new Intent(this, PreviewImgActivity.class);
                    intent.putStringArrayListExtra(Constant.INTENT_DATA, listA);
                    startActivity(intent);
                }
                break;
        }
    }

    @Override
    public void success(Object object, int tag) {
        if (object instanceof DaySignDetailsResponse.ResponseBean) {
            bean = (DaySignDetailsResponse.ResponseBean) object;
            GlideUtils.loadImageView(this, ivDaySignImg, bean.getImgUrl());
            if (!TextUtils.isEmpty(bean.getImgExplain())) {
                txtDaySignTitle.setText(bean.getImgExplain());
            }
            txtDaySignContent.setText(bean.getDailyContent());
            String today = DateUtils.getToday("yyyy年MM月dd日");
            shareDataLayout.setText(today + " " + DateUtils.getTodayWeek());
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }

    @Override
    public void deleteDaySignSuccess() {
        setResult(RESULT_OK);
        finish();
    }
}
